import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/values/MyColors.dart';
import 'package:flutter_screenutil/src/size_extension.dart';

/**
 * @Description: Dialog工具类
 * @Author: 下雨了
 * @CreateDate: 2021年11月24日11:41:23
 * @UpdateUser:  更新者
 * @UpdateDate: 2021年11月24日11:41:23
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */

///定义一个top-level（全局）变量，
var dialogUtils = DialogUtils();

class DialogUtils {
  // 私有构造函数
  DialogUtils._internal();
  // 保存单例
  static final DialogUtils _dialogUtils = DialogUtils._internal();
  // 工厂构造函数
  factory DialogUtils() => _dialogUtils;


  /// 根据系统 区分android和ios端弹窗提醒
  void show({
    required BuildContext context,
    required String title,
    required String content,
    String okText = "确定",
    String cancelText = "取消",
    VoidCallback? callbackOk,
    VoidCallback? callbackCancel,
    bool single = true, // 是否显示 一个按钮
  }) {
    if (Platform.isAndroid) {
      showAlertDialog(
          context: context,
          title: title,
          content: content,
          okText: okText,
          cancelText: cancelText,
          callbackOk: callbackOk,
          callbackCancel: callbackCancel,
          single: single,
      );
    } else {
      showCustomCancelDialog(
        context: context,
        title: title,
        content: content,
        okText: okText,
        cancelText: cancelText,
        callbackOk: callbackOk,
        callbackCancel: callbackCancel,
        single: single,
      );
    }
  }

  /// 显示  AlertDialog
  void showAlertDialog({
    required BuildContext context,
    required String title,
    required String content,
    required String okText,
    required String cancelText,
    VoidCallback? callbackOk,
    VoidCallback? callbackCancel,
    required bool single, // 是否显示 一个按钮
  }) {
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (context) {
        return AlertDialog(
          title: Text(title),
          titleTextStyle: TextStyle(color: Colors.black, fontSize: 16.sp, fontWeight: FontWeight.w500),
          contentPadding: EdgeInsets.fromLTRB(24.r, 10.r, 24.r, 0.0),
          content: Text(content),
          contentTextStyle: TextStyle(fontSize: 14.sp, color: MyColors.FF666666, fontWeight: FontWeight.w500),
          actions: <Widget>[
            Visibility(
              visible: single,
              child: TextButton(
                child: Text(cancelText),
                onPressed: () {
                  Navigator.of(context).pop('cancel');
                  callbackCancel?.call();
                },
              ),
            ),
            TextButton(
              child: Text(okText),
              onPressed: () {
                Navigator.of(context).pop('ok');
                callbackOk?.call();
              },
            ),
          ],
        );
      },
    );
  }

  /// 显示自定义 确认按钮 风格类似ios
  showCustomCancelDialog({
    required BuildContext context,
    required String title,
    required String content,
    String okText = "确定",
    String cancelText = "取消",
    VoidCallback? callbackOk,
    VoidCallback? callbackCancel,
    required bool single, // 是否显示 一个按钮
  }) {
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (context) {
      return Dialog(
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(4.r),
        ),
        child: IntrinsicHeight(
          child: Column(
            children: [
              Container(
                  margin: EdgeInsets.only(top: 17.5.r),
                  child: Text(title, style: TextStyle(color: Colors.black, fontSize: 16.sp, fontWeight: FontWeight.w500),)),
              Container(
                margin: EdgeInsets.only(top: 11.5.r, bottom: 12.r, left: 16.r, right: 16.r),
                child:  Text(content, style: TextStyle(color: MyColors.FF666666, fontSize: 14.sp, fontWeight: FontWeight.w500),),
              ),
              Divider(thickness: 0.5.r, height: 0, color: MyColors.FFDDDDDD,),
              Container(
                height: 45.r,
                child: Row(
                  children: [
                    Visibility(
                      visible: single,
                      child: Expanded(
                        child: Row(
                          children: [
                            Expanded(
                              child: SizedBox(
                                height: 44.r,
                                child: TextButton(
                                  child: Text(cancelText, style: TextStyle(color: MyColors.FF333333, fontSize: 14.sp, fontWeight: FontWeight.w500), ),
                                  style: ButtonStyle(
                                    padding: MaterialStateProperty.all(EdgeInsets.all(0)),
                                  ),
                                  onPressed: () {
                                    Navigator.of(context).pop('cancel');
                                    callbackCancel?.call();
                                  },
                                ),
                              ),
                            ),
                            Container(
                              width: 0.5.r,
                              height: 43.r,
                              color: MyColors.FFDDDDDD,
                            ),
                          ],
                        ),
                      ),
                    ),
                    Expanded(
                      child: SizedBox(
                        height: 44.r,
                        child: TextButton(
                          child: Text(okText, style: TextStyle(color: MyColors.FF2763B9, fontSize: 14.sp, fontWeight: FontWeight.w500),),
                          style: ButtonStyle(
                            padding: MaterialStateProperty.all(EdgeInsets.all(0)),
                          ),
                          onPressed: () {
                            Navigator.of(context).pop('ok');
                            callbackOk?.call();
                          },
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        )
      );
    },
    );
  }

  /// 隐私协议弹窗
  showFirstDialog(
      {required BuildContext context,
      required VoidCallback callbackOk,
      required VoidCallback callbackCancel,
      required GestureTapCallback onTap,}) {
    showDialog(context: context, barrierDismissible: false, builder: (context) {
      return Dialog(
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(4.r),
        ),
        child: IntrinsicHeight(
          child: Column(
            children: [
              Container(
                margin: EdgeInsets.only(top: 20.r),
                child: Center(
                  child: Text("欢迎使用效能监督", style: TextStyle(color: MyColors.FF010101, fontSize: 16.sp),),
                ),
              ),
              Container(
                margin: EdgeInsets.all(15.r),
                height: 230.r,
                child: SingleChildScrollView(
                  child: Column(
                    children: [
                      RichText(text: TextSpan(
                          children: [
                            TextSpan(
                                text: "《效能监督隐私权限政策》",
                                style: TextStyle(color: MyColors.FFC30802, fontSize: 14.sp),
                                recognizer: TapGestureRecognizer()
                                  ..onTap = () {
                                    onTap();
                                  }
                            ),
                            TextSpan(
                              text: "将帮助您了解如何保护您的个人信息和隐私，以及效能监督如何收集、处理您的个人信息。",
                              style: TextStyle(color: MyColors.FF666666,  fontSize: 14.sp),
                            )
                          ]
                      )),
                      SizedBox(height: 10.r,),
                      Text("我们可能会申请系统设备权限，以及收集其他设备信息如网络设备硬件地址、日志信息等，用于识别设备，进行信息推送。但需要您的同意。",
                        style: TextStyle(color: MyColors.FF666666, fontSize: 14.sp),),
                      SizedBox(height: 10.r,),
                      Text("我们可能会申请麦克风和摄像头权限，用于语音或者视频通讯。但需要您的同意。",
                        style: TextStyle(color: MyColors.FF666666, fontSize: 14.sp),),
                      SizedBox(height: 10.r,),
                      Text("上述权限以及相册（存储）、GPS、悬浮窗等权限均不会默认或强制开启收集信息。",
                        style: TextStyle(color: MyColors.FF666666, fontSize: 14.sp),),
                    ],
                  ),
                ),
              ),
              Divider(height: 1.0, color: MyColors.FFE6E6E6),
              Container(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    TextButton(
                      child: Text("退出应用", style: TextStyle(color: MyColors.FF333333, fontSize: 14.sp),),
                      style: ButtonStyle(
                          padding: MaterialStateProperty.all(EdgeInsets.all(0)),
                          minimumSize: MaterialStateProperty.all(Size(100.r, 40.r))
                      ),
                      onPressed: () {
                        callbackCancel();
                      },
                    ),
                    Container(
                      width: 0.5,
                      height: 40,
                      color: MyColors.FFDDDDDD,
                    ),
                    TextButton(
                      child: Text("同意", style: TextStyle(fontSize: 14.sp, fontWeight: FontWeight.w600),),
                      style: ButtonStyle(
                          padding: MaterialStateProperty.all(EdgeInsets.all(0)),
                          minimumSize: MaterialStateProperty.all(Size(100, 40))
                      ),
                      onPressed: () {
                        Navigator.of(context).pop('ok');
                        callbackOk();
                      },
                    ),
                  ],
                ),
              )
            ],
          ),
        ),
      );
    });
  }
}